<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="com.mongodb.BasicDBObject"%>
<%@page import="com.mongodb.DBObject"%>
<%@page import="pagerank.controller.Data"%>
<%@page import="com.mongodb.DBCollection"%>
<%@page import="pagerank.controller.PageRankService"%>
<%@page import="sun.misc.BASE64Decoder"%>
<%@ page import="java.io.*" %>
<%
response.setHeader("Cache-Control", "public, max-age=2592000");
response.setHeader("Expires", new Date(Calendar.getInstance().getTime().getTime()+10*24*60*60*1000).toGMTString());
DBCollection coll = Data.data("pagerank");

String domain = request.getParameter("url");
String type = request.getParameter("f");

DBObject pagerank = coll.findOne(new BasicDBObject("name",domain));
int pr = 0;
if(pagerank != null)
{
	if(Long.parseLong(pagerank.get("date").toString()) < (new Date().getTime()-30*24*60*60*1000))
	{
		long start = System.currentTimeMillis();
		PageRankService prService = new PageRankService();
	
		pr = prService.getPR(domain);
		
		BasicDBObject info = new BasicDBObject();
		info.put("pr", pr);
		BasicDBObject set = new BasicDBObject("$set", info);
		coll.update(pagerank, set);
	} else {
		pr = Integer.valueOf(pagerank.get("pr").toString());
	}
} else {
	long start = System.currentTimeMillis();
	PageRankService prService = new PageRankService();

	pr = prService.getPR(domain);
	BasicDBObject add = new BasicDBObject();
	add.put("pr", pr);
	add.put("name", domain);
	add.put("date", new Date().getTime());
	coll.insert(add);	
}
if(pr==-1)
{
	pr = 0;	
}
if(type == null)
{
	if(pr != 6 && pr != 9)
	{
		response.addHeader("Content-type","image/gif");
		response.addHeader("Content-length","272");
	}
	switch (pr){
		case 0:
			BASE64Decoder decoder = new BASE64Decoder();
			String encodedBytes = new String("R0lGODlhRgAUAKIAAKSkpL+/v9zc3JSUlPT09AAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPVaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"hkAAgHAsCha7YwyGDY6+ONk7bGACvIDlS7sjSsnD6/56+lqggc"+

					"AgZ+gDlHYHZiGk9AaItANzoLbVsABwJwU2RLelKOUHiHVJAKkg"+

					"aUloQ7IWM/JXwGpKaXJXqsraKvB5OVsrW9IreCgX+8RcUTaVZZ"+

					"AVhbqDa+vamuwhaDznvQ0chqAG9cK4/ZrNK3FF17xulc2Mbn4u"+

					"KpP+rz9BMJADs="
					);
			byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 1:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhRgAUAKIAAACZM6Wlpdzc3L29vZSUlAAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPLaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"h0CA4Bh67ZwiCDX66PF07aHgNLVrUbS7/K6bNb7lqqCgJmnTt6"+

					"5WAaT0BmhEA3OgsBBwNXfAtbhyxSk0h2h1B0gFSJeQQGjlorIW"+

					"E/JXgMoWwml6YnnalYojuutSKwC6p9tEW9E2cPupCjBgDGALY/"+

					"vLgKwka8x8jJJsu+kTfR09TARde+3w/cONfa07TK4OnqEgkAOw"+

					"=="

					
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 2:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhRgAUAKIAAN3d3b6+vqmpqZeXlwCZMwAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPUaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"hkCACWgEPXbGGQQXDXxxMnbY2BBXvQ5o4xLxAqny+95npVETgA"+

					"DAJ+KjtOU2BkQGVhhol7CgMDgIJvhF9SLHKXT5h4jFQ6D1gCgy"+

					"ViPyWOj1hulCZ1pyegDKsPXLC3Hqm0tSuyRb+1Fo59GH+twgTJ"+

					"ygS4qGizGcYLtjfLys2xwr4U1AbWydghOzfbE93A6FvawNTh2O"+

					"M/6fLzEwkAOw=="
			
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 3:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhRgAUAKIAAKqqqr+/v5iYmObm5gCZMwAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPXaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"hkAAaHgcCha7YwyGDY6+ONk7aGwILV5Y6xLxAqny+/53pVcQ0Y"+

					"sAAqO05TYWVAZmKFiHsNWFxwd3VSUoh0aHZUbhUHgYIlYz8ljQ"+

					"sBWFtvOzSHoiKbDQBZn620Ho0BB36xA7OvRb9cFrcWuJ29BgTJ"+

					"ysvJtRyqpMW5kNDM1gTOG9C+BgIAqMfXzNka28BdwsDqwTfcEu"+

					"jk5Ko/6/X2EwkAOw=="
			
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 4:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhRgAUAKIAAJmZmdvb26+vr/f39wCZMwAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPOaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"h8BAIOXbOFQQa9Wx8PnLRBApbsMcYFQtvuJZcMrzIEH9XOOfWK"+

					"gWNffYB2CgADaDo5a3JwUlKAb2VxVIoLAQM7ajshYD8lhXgCmn"+

					"omcJ8nljtXFgEAi5yoshyFG6MLWmlFuxJmELqwHwTDxMXGBLMY"+

					"nIU5qkTCx9HDyTZpzhO5N9LS1NbWvNm84g++Rdnd1LEw4+ztEw"+

					"kAOw=="
			
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 5:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhRgAUAKIAAOvr69jY2JeXl7GxsQCZMwAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPUaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"hkDALYwEC1Uy6nySlL+kUGa9VFALPNHWMt8Dgub4aB5LQCcBgI"+

					"/FxieHI+YneHZzYOBwB/DzqCXnN2X2dRdTN6AxiNgUJAPyWajA"+

					"N8AZ40haEeegYCAiuoq7Mco20WqEW6E4pWFqUHp247NwTGx8jJ"+

					"ybTErWsWwsMfytTUzL0Nf7CLK8XV38bXuDpFkLi76Ivju+a07k"+

					"Oz6fLzEwkAOw=="
			
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 6:
			response.setContentType("image/gif");
			OutputStream o = response.getOutputStream();
		    InputStream is = new FileInputStream(new File("/root/images/showimage.png"));
		    byte[] buf = new byte[32 * 1024]; 
		    int nRead = 0;
		    while( (nRead=is.read(buf)) != -1 ) {
		        o.write(buf, 0, nRead);
		    }
		    o.flush();
		    o.close();
			break;
		case 7:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhRgAUAKIAAKioqMDAwODg4JqamgCZMwAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPOaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"hcBARYbEC1Uy6nySlL+kUGa1WFICPgiptmnhf4bNHhtsYAwF8P"+

					"3EBggj5iYXKDeQ8AB1uATmRSkGVRglQ6DmsPOiVxPyVpVoyaKz"+

					"SEnh6gapeAp60iqwoDB22jO7BFuA6JDIuNuqQGBMLDxMXGxp+7"+

					"ob6swcfP0MLJFqkTm9TR2cgnyinXt7nhttUS367nGLY/4uztEw"+

					"kAOw=="
			
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 8:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhRgAUAKIAANbW1uvr66CgoLm5uQCZMwAAAISEhP///y"+

					"H5BAAAAAAALAAAAABGABQAAAPgaLrc/jDK2Y69OOvN+zVep4Rk"+

					"SYJUNR5FW5iZ+5Lzd6TMDbJWDfM9WgaFG61mL9nF1fPVkkxmEE"+

					"hkDAKHwMCha7YwyGDY6+ONk7aGwDKwCFS7sjSsnD6/56+lqmhv"+

					"/XBTdoNlQGZihGkMawAGAAdvOUdgUix6lU+WeINUOlYXW4FChj"+

					"8nngprB22QojSFpSKnjqyMrbC3HHyzW7WSO7JFwVx7sm0BAlih"+

					"C117BM7P0NHS09C5igyPFo2tBtTe39HWxA8C5Q/MN+Dq3uI3wB"+

					"PowvLn48HouPgYvz/z/f4TCQAAOw=="			
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();
			break;
		case 9:
			response.setContentType("image/gif");
			OutputStream os = response.getOutputStream();
		    InputStream iss = new FileInputStream(new File("/root/images/showimage9.png"));
		    byte[] bufs = new byte[32 * 1024]; 
		    int nReads = 0;
		    while( (nReads=iss.read(bufs)) != -1 ) {
		        os.write(bufs, 0, nReads);
		    }
		    os.flush();
		    os.close();
			break;
		case 10:
			decoder = new BASE64Decoder();
			encodedBytes = new String("R0lGODlhSAAWALMAAAAAAP///wCZM/f39+zs7OHh4dPT08zMzMDAwLW1taqqqpmZmYmJiYSEhP///wAAACH5BAEAAA4ALAAAAABIABYAAAT/MMhJq704632b/2AojmRpnqDUcGybre4Ew3HNrmgYzBLgA7bAD8ghyna5z07Va9qMxkx0mVTyhM3h5NeLQoVcrpOI+xwYoAPBgPYs39rtGEsH+ypAor5ebhgCCx8EAQiDVkx1XXtYcXRQd4tiVAwDA4AeDEgrCW5XXnOMFl97kXw7CgorgQ0JA2YFnYhekmK0clqldH2qHoUfvjiIG4tBGruXDcDJBLFvRVPFHZq8y7/Mwc7R2kwpqwuaAwjNNFXlJ0fdSgULCJpv5ALx8vP09fb38+TTyA2UEqnjZOAbSHCgPnPvBBZcyPBguYQqzEkUge7hlW3baMSYMbGjxxIYBENijAAAOw=="
			
					);
			decodedBytes = decoder.decodeBuffer(encodedBytes);
			response.getOutputStream().write(decodedBytes);
			response.flushBuffer();	
			break;
	}
} else {
	if(type.equals("xml"))
	{
		response.addHeader("Content-type","text/xml");
		out.print("<?xml version='1.0' encoding='ISO-8859-1'?>");
		out.print("<pr>");
		out.print(pr);
		out.print("</pr>");
	}
	if(type.equals("json"))
	{
		out.print( "{ \"url\":\"http://"+domain+"\" ,\"pagerank\":\""+pr+"\"}");
	}
	if(type.equals("text"))
	{
		out.print(pr);
	}
}
%>